package com.embracesource.business.exception;

import com.embracesource.business.util.R;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 全局异常处理
 * @Author: GNERV  LiGen
 * @CreateDate: 2018/11/22
 */
@ControllerAdvice
public class GlobalExceptionHandler {


    /**
     * 权限异常处理
     *
     * @param req
     * @param e
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = UnauthorizedException.class)
    @ResponseBody
    public R permissionErrorHandler(HttpServletRequest req, Exception e) {
        return R.error(R.OK_401);
    }

    /**
     * 通用异常处理
     *
     * @param req
     * @param e
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public R defaultErrorHandler(HttpServletRequest req, Exception e) {
        return R.error(R.ERROR_500);
    }
}
